package com.zxc.demo.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxc.demo.dao.UserQxDao;
import com.zxc.demo.model.*;
import com.zxc.demo.service.GuiGeService;
import com.zxc.demo.service.HuoJiaService;
import com.zxc.demo.service.SpService;
import com.zxc.demo.service.ZybDcUserService;
import com.zxc.demo.util.AESUtil;
import com.zxc.demo.util.CookieUtil;
import com.zxc.demo.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.jsoup.helper.StringUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 消费记录表zyb_dc_consume_record(ZybDcConsumeRecord)表控制层
 *
 * @author makejava
 * @since 2021-02-22 11:25:35
 */
@RestController
@Log4j2
@RequestMapping("product")
@Api(value = "商品接口",tags = {"商品接口"})
@Validated
public class ProductController {
    @Resource
    private GuiGeService guiGeService;
    @Resource
    private SpService spService;
    @Resource
    private HuoJiaService huoJiaService;
    @GetMapping("getSpList")
    @ApiOperation(value = "获取商品列表",notes = "获取商品列表")
    public R getSpList(HttpServletRequest request) {
        List<HuoJia> list = huoJiaService.list(new LambdaQueryWrapper<HuoJia>()
                .eq(HuoJia::getIsDelete, 0));
        Set<Integer> set = list.stream().map(HuoJia::getId).collect(Collectors.toSet());
        List<Sp> splis = spService.list(new LambdaQueryWrapper<Sp>()
                .in(set!=null&&set.size()>0,Sp::getHuoJiaId,set)
                .eq(Sp::getIsDelete,0));
        Set<Integer> SPIDS = splis.stream().map(Sp::getId).collect(Collectors.toSet());
        List<GuiGe> guiGeList = guiGeService.list(new LambdaQueryWrapper<GuiGe>().in(SPIDS!=null&&SPIDS.size()>0,GuiGe::getSpId,SPIDS));
        Map<Integer, List<GuiGe>> ggmap = guiGeList.stream().collect(Collectors.groupingBy(GuiGe::getSpId));
        for (Sp spli : splis) {
            List<GuiGe> guiGes = ggmap.get(spli.getId());
            if (guiGes!=null){
                spli.setGuiGeList(guiGes);
            }else {
                spli.setGuiGeList(new ArrayList<>());
            }
        }
        Map<Integer, List<Sp>> spmap = splis.stream().collect(Collectors.groupingBy(Sp::getHuoJiaId));
        for (HuoJia huoJia : list) {
            List<Sp> sps = spmap.get(huoJia.getId());
            if (sps!=null){
                huoJia.setSplist(sps);
            }else {
                huoJia.setSplist(new ArrayList<>());
            }
        }
        return R.success(list);
    }
    /**
     * 通用查询
     * @param queryVO
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public R query(QueryVO queryVO){
        Page page = new Page(queryVO.getNowPage(),queryVO.getPageSize());
        QueryWrapper<Sp> wrapper = new QueryWrapper<>();
        if (queryVO.getShelfId()!=null){
            wrapper.eq("huo_jia_id",queryVO.getShelfId());
        }
        if (!StringUtils.isEmpty(queryVO.getKey())){
            wrapper.like("name",queryVO.getKey());
        }
        if (!StringUtils.isEmpty(queryVO.getRankKey())){
            if ("ASC".equals(queryVO.getRankType())){
                wrapper.orderByAsc(queryVO.getRankKey());
            }
            if ("DESC".equals(queryVO.getRankType())){
                wrapper.orderByDesc(queryVO.getRankKey());
            }
        }else {
            queryVO.setRankKey("id");
            wrapper.orderByDesc(queryVO.getRankKey());
        }
        wrapper.eq("is_delete",0);
        Page<Sp> rpage = spService.page(page, wrapper);
        List<Sp> records = rpage.getRecords();
        if (records.size()==0){
            return R.success(rpage);
        }
        return R.success(rpage);
    }
    @PostMapping ("setHuoJia")
    @ApiOperation(value = "设置货架",notes = "设置货架")
    public R setHuoJia(@RequestBody HuoJia huoJia) {
        boolean b = huoJiaService.saveOrUpdate(huoJia);
        return R.success(huoJia);
    }
    @PostMapping ("setSp")
    @ApiOperation(value = "设置商品",notes = "设置商品")
    public R setSp(@RequestBody Sp sp) {
        boolean b = spService.saveOrUpdate(sp);
        return R.success(sp);
    }
    @PostMapping ("setGuiGe")
    @ApiOperation(value = "设置规格",notes = "设置规格")
    public R setGuiGe(@RequestBody GuiGe guiGe) {
        boolean b = guiGeService.saveOrUpdate(guiGe);
        return R.success(guiGe);
    }
}
